package com.wey.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wey.vo.DeepSeekResponse;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
@RefreshScope
public class DeepSeekService {

    @Value("${deepseek.appkey}")
    private String appkey;
//    private static final String appkey = "sk-61089ff27d7847619a9556b29ec99441";

   public DeepSeekResponse deepSeekClient() throws IOException {
       OkHttpClient client = new OkHttpClient.Builder()
               .connectTimeout(30, TimeUnit.SECONDS) // 连接超时时间
               .readTimeout(30, TimeUnit.SECONDS)    // 读取超时时间
               .writeTimeout(30, TimeUnit.SECONDS)   // 写入超时时间
               .build();
       MediaType mediaType = MediaType.parse("application/json");
       RequestBody body = RequestBody.create(mediaType, "{\n" +
               "    \"messages\": [\n" +
               "        {\n" +
               "            \"content\": \"你是一个NBA篮球分析员\",\n" +
               "            \"role\": \"system\"\n" +
               "        },\n" +
               "        {\n" +
               "            \"content\": \"帮我分析一下啊NBA球队中掘金队今年的得冠趋势\",\n" +
               "            \"role\": \"user\"\n" +
               "        }\n" +
               "    ],\n" +
               "    \"model\": \"deepseek-chat\",\n" +
               "    \"stream\": false\n" +
               "}");
       Request request = new Request.Builder()
               .url("https://api.deepseek.com/chat/completions")
               .method("POST", body)
               .addHeader("Content-Type", "application/json")
               .addHeader("Accept", "application/json")
               .addHeader("Authorization", "Bearer " + appkey)
               .build();
       Response response = client.newCall(request).execute();
       return JSONObject.parseObject(response.body().string(), DeepSeekResponse.class);
    }

}
